const Router = require('@koa/router');
const router = new Router();
const { resolve } = require('path');
const fs = require('fs');
const { renderToString } = require('@vue/server-renderer');
const serverBundle = require('../../../dist/server-bundle.js').default;
const template = fs.readFileSync(resolve(__dirname,"../../../dist/index.html"),'utf-8');
// 注水、脱水
const renderState=(state,windowKey)=>{
    return `<script>window.${windowKey}=${JSON.stringify(state)}</script>`
}
module.exports = (app)=>{
    router.get(["/","/about"],async(ctx,next)=>{
        //ctx.body = "服务端路由成功";
        const {app:appComp,router:r,store} = serverBundle();
        r.push(ctx.req.url);
        await r.isReady();
        let appContent = await renderToString(appComp);
        appContent = `<div id="app">${renderState(store.state,"__INITIAL_STATE__")}${appContent}</div>`;
        console.log(appContent);
        let html = template.replace(`<div id="app"></div>`,appContent);
        ctx.body = html;
    });
    app.use(router.routes()).use(router.allowedMethods());
}